document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");

var action_indicator_type_all="/indicator/type/all.do";
var action_indicator_sameType = "/indicator/sameType.do";

var page = 1;
var pageSize = 30;

function loadForm(){
    $('#formid').combobox({
        onLoadSuccess: function () {
            loadIndicatorType();
        },
        onSelect: function(index, row){
            loadIndicatorType();
        }
    });
}

function loadFormAndIndicatorTypeAndIndicator(){
    $('#formid').combobox({
        onLoadSuccess: function () {
            loadIndicatorTypeAndIndicator();
        },
        onSelect: function(index, row){
            loadIndicatorTypeAndIndicator();
        }
    });
}

function loadFormAndOnlyIndicatorType(){
    $('#formid').combobox({
        onLoadSuccess: function () {
            loadOnlyIndicatorType();
        },
        onSelect: function(index, row){
            loadOnlyIndicatorType();
        }
    });
}

//获取领域数据
function loadOnlyIndicatorType() {

    var formsid=$('#formid').combobox('getValue');
    $.get(action_indicator_type_all, {'formsid':formsid}, function(json) {
        json.data.unshift({
            id : -1,
            name : "全部"
        });
        $('#indicatorTypeSelect').combobox({
            textField:"name",
            valueField:"id",
            data:json.data,
            multiple:true,
            editable:false,
            panelHeight:"auto",
            panelWidth:'200px',
            panelAlign:'left',
            formatter:function(row){
                var opts;
                if(row.select=="true"){
                    row.selected=true;
                    opts = "<input type='checkbox' name='year' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
                }else{
                    opts = "<input type='checkbox' name='year' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
                }
                return opts;
            },
            onLoadSuccess : function() { // 数据加载完毕事件
                var a = $('#indicatorTypeSelect').combobox('getData');
                if (a.length > 0) {
                    $("#indicatorTypeSelect").combobox('select', a[0].id);
                }
            },
            onSelect:function(rec) {
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=true;
            },
            onUnselect:function(rec){
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=false;
            }
        });
    }, "json");
}

//获取领域数据
function loadIndicatorTypeAndIndicator() {

    var formsid=$('#formid').combobox('getValue');
    $.get(action_indicator_type_all, {'formsid':formsid}, function(json) {
        json.data.unshift({
            id : -1,
            name : "全部"
        });
        $('#indicatorTypeSelect').combobox({
            async : false,
            method : 'GET',
            data : json.data,
            valueField : 'id',
            textField : 'name',
            fitColumns:false,
            collapsible: true,
            singleSelect : false,// 是否单选
            checkOnSelect: true,//选中行后,checkbox选中
            selectOnCheck: true,//选中行后,checkbox选中
            // columns: [
            //     defineTableIndicatorTypeNames
            // ],
            multiple:true,
            editable:false,
            formatter:function(row){
                var opts;
                if(row.selected==true){
                    opts = "<input type='checkbox' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
                }else{
                    opts = "<input type='checkbox' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
                }
                return opts;
            },
            onLoadSuccess : function() { // 数据加载完毕事件
                var a = $('#indicatorTypeSelect').combobox('getData');
                if (a.length > 0) {
                    $("#indicatorTypeSelect").combobox('select', a[0].id);
                }
            },
            //二级联动，根据领域获取相应指标数据
            onSelect:function(rec) {
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=true;
                loadIndicator();
            },
            onUnselect:function(rec){
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=false;
                loadIndicator();
            },
            onSelectAll: function(index, row){
                loadIndicator();
            },
            onUnselectAll: function(index, row){
                loadIndicator();
            }
        });
    }, "json");
}

//获取领域数据
function loadIndicatorType() {

    var formsid=$('#formid').combobox('getValue');
    $.get(action_indicator_type_all, {'formsid':formsid}, function(json) {
        // json.data.unshift({
        //     id : -1,
        //     name : "全部"
        // });
        $('#indicatorTypeSelect').combobox({
            async : false,
            method : 'GET',
            data : json.data,
            valueField : 'id',
            textField : 'name',
            fitColumns:false,
            collapsible: true,
            singleSelect : false,// 是否单选
            checkOnSelect: true,//选中行后,checkbox选中
            selectOnCheck: true,//选中行后,checkbox选中
            // columns: [
            //     defineTableIndicatorTypeNames
            // ],
            multiple:true,
            editable:false,
            formatter:function(row){
                var opts;
                if(row.selected==true){
                    opts = "<input type='checkbox' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
                }else{
                    opts = "<input type='checkbox' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
                }
                return opts;
            },
            onLoadSuccess : function() { // 数据加载完毕事件
                var a = $('#indicatorTypeSelect').combobox('getData');
                if (a.length > 0) {
                    $("#indicatorTypeSelect").combobox('select', a[0].id);
                }
            },
            //二级联动，根据领域获取相应指标数据
            onSelect:function(rec) {
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=true;
                // queryByIndicatorType();
            },
            onUnselect:function(rec){
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=false;
                // queryByIndicatorType();
            },
            onSelectAll: function(index, row){
                // queryByIndicatorType();
            },
            onUnselectAll: function(index, row){
                // queryByIndicatorType();
            }
        });
    }, "json");
}

function loadIndicator(curPage, pageSize){

    if(pageSize==null){
        pageSize=30;
    }
    if(curPage==null){
        curPage=1;
    }
    var indicatorType = $('#indicatorTypeSelect').combobox('getValues')+"";
    var formsid=$('#formid').combobox('getValue');
    var iname = $('#iname').textbox('getValue');

    $.get(action_indicator_sameType, {"iname":iname, "type":indicatorType, "formsid":formsid, "page":curPage, "pageSize":pageSize}, function (json) {

        if(json.code!=200){
            $('#ttIndicator').datagrid( {data:[]});
            return;
        }
        isTrue = true;
        var data=json.data;
        $('#ttIndicator').datagrid( {
            data:data.results,
            fitColumns:true,
            columns: [
                defineTableIndicatorNames
            ],
            pagination:true,
            onLoadSuccess: function () {
                loadIndicatorList();
            },
            onSelect: function(index, row){
                loadIndicatorList();
            },
            onSelectAll: function(index, row){
                loadIndicatorList();
            },
            onUnselect: function (rowIndex, rowData) {
                moveCache(rowData.id);
                loadIndicatorList();

            },
            onUnselectAll: function(index, row){
                loadIndicatorList();
                ttIndicatorSelectCache=[];
            }
        });

        var total=data.size;
        var dataLength=data.results.length;
        var totalPage = Math.ceil(total / pageSize);
        var start = (curPage-1) * pageSize;
        var pg = $("#ttIndicator").datagrid("getPager");
        $(pg).pagination({
            total: total,
            pageList: [30, 500, 1000, 1500],
            pageSize: pageSize,
            pageNumber: curPage,
            beforePageText: '第',
            afterPageText: '页    共 ' + totalPage + ' 页',
            displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
            onSelectPage: function (curPage, pageSize) {
                loadIndicator(curPage,pageSize)
            },
            onChangePageSize: function (ps) {
                pageSize = ps;
            }
        });
    },'json');
}

// 用于保存选中的数据（翻页）
var ttIndicatorSelectValue;
function loadIndicatorList(){

    var row=$('#ttIndicator').datagrid('getSelections');
    ttIndicatorSelectCache = putCache(row);

    $('#ttIndicatorSelect').datagrid( {
        data:ttIndicatorSelectCache,
        fitColumns:true,
        columns: [
            defineTableIndicatorOnlyNames
        ]
    });

}

var allYear="/resources/js/views/allYear.do";
function onlyLoadYear(){
    $.get(allYear, function (json) {

        $("#comboboxYear").combobox({
            textField:"text",
            valueField:"id",
            width:'100px',
            height:'28px',
            data:json,
            multiple:true,
            editable:false,
            panelHeight:"auto",
            panelWidth:'100px',
            panelAlign:'left',
            formatter:function(row){
                var opts;
                if(row.select=="true"){
                    row.selected=true;
                    opts = "<input type='checkbox' name='year' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.text+"</input>";
                }else{
                    opts = "<input type='checkbox' name='year' id='"+row.id+"' value='"+row.id+"'>"+row.text+"</input>";
                }
                return opts;
            },
            onSelect:function(rec) {
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=true;
            },
            onUnselect:function(rec){
                oCheckbox = document.getElementById(rec.id);
                oCheckbox.checked=false;
            }
        });
    },'json');
}

function moveCache(aimId){
    var tempTtIndicatorSelectCache =[];
    let j = 0;
    for (let i = 0; i < ttIndicatorSelectCache.length; i++) {
        if(aimId != ttIndicatorSelectCache[i].id){
            tempTtIndicatorSelectCache[j] = ttIndicatorSelectCache[i];
            j++;
        }
    }
    ttIndicatorSelectCache = tempTtIndicatorSelectCache;
}

function putCache(row){
    for (let i = 0; i < row.length; i++) {
        if(!imHere(row[i].id, ttIndicatorSelectCache)){
            ttIndicatorSelectCache[ttIndicatorSelectCache.length]=row[i];
        }
    }
    return ttIndicatorSelectCache;
}

function imHere(idAim, row){
    for (let i = 0; i < row.length; i++) {
        if(idAim==row[i].id){
            return true;
        }
    }
    return false;
}